/**
 * @Author 韦建韩
 * @Date 2022/5/15
 */
package com.example.devops.web.controller.applicationAdmin;

import com.example.devops.Manage.UserManager.UserInfoManage;
import com.example.devops.dao.UserMapper;
import com.example.devops.entity.common.Result;
import com.example.devops.entity.pojo.User;
import com.example.devops.exception.ValidationException;
import com.example.devops.service.IProjectService;
import com.example.devops.service.impl.PublishHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@RestController
@RequestMapping("appPublish")
public class AppPublishController {

    @Autowired(required = false)
    IProjectService projectService;
    @Autowired
    UserMapper userMapper;

    @Autowired
    PublishHistoryService publishHistoryService;

    public Result publish() {
        return null;
    }

    @RequestMapping("recentlyPublish")
    public Result recentlyPublish() {
        return projectService.recentlyPublish();
    }

    @RequestMapping("publishMirror")
    public Result publishMirror(Long id) throws ParseException {
        User user = UserInfoManage.get();
        User user1 = userMapper.selectById(user.getId());
        if (user1 != null && user1.getPrivilege() != null) {
            if (user1.getPrivilege().contains("2")) {
                return projectService.projectPublish(id);
            } else {
                throw new ValidationException("你没有发布项目的权限");
            }
        } else {
            throw new ValidationException("你没有发布项目的权限");
        }

    }

    @RequestMapping("getPublishInstance")
    public Result getPublishInstance(Long id) {
        if (id == 1) {
            return projectService.findAllInstance();
        }
        return projectService.getPublishInstance(id);
    }

    @RequestMapping("getPublishHistory")
    public Result getPublishHistory() {
        return publishHistoryService.getPublishHistory();
    }
}
